package org.demo.test.thread._volatile;

public class VolatileTest02 {

//    volatile boolean running = true;
    boolean running = true;
    volatile boolean running1 = true;
    byte[] arr = new byte[1200000];
    void m() {
        System.out.println("m start.");
        boolean a =  running;
//        while (running){
        while (a){
            // 为什么这个地方操作volatile变量就能保证running的可见性？？
            /// running1 = false;
            a = running;
        }
        System.out.println("m end.");
    }

    public static void main(String[] args) {
        VolatileTest02 tt = new VolatileTest02();
        Thread t = new Thread(tt::m, "t1");
        t.start();

        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        tt.running = false;
        tt.running1 = false;
//        for(byte b : tt.arr){
//            b = '1';
//        }
        System.out.println("停止了");
        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }
}
